package com.coocaa.ops.plus.dashboard.v1.codegen.service.impl;

import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.tool.http.HttpSessionUtil;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.CodeGenConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.PiscesTemplateConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.TableInfoModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.output.FrontConfigInfo;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.query.DbTableQueryModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.provider.impl.PiscesCodeGenProviderImpl;
import com.coocaa.ops.plus.dashboard.v1.codegen.service.ICodeGenService;
import com.coocaa.ops.plus.dashboard.v1.codegen.util.SqlUtil;
import com.coocaa.ops.plus.dashboard.v1.codegen.validator.CodeGenValidator;
import com.coocaa.ops.plus.dashboard.v1.codegen.validator.DbValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author : bijiahao
 * @date : 2019/4/20.
 * @description:
 */
@Service
public class CodeGenServiceImpl implements ICodeGenService {
    @Autowired
    private PiscesCodeGenProviderImpl piscesCodeGenProvider;

    @Override
    public R execCodeGen(CodeGenConfigModel codeGenConfigModel) throws Exception {
        this.handleCodeGen(codeGenConfigModel);
        //代码生成之后的处理
        handleAfterCodeGen(codeGenConfigModel);
        return R.ok().data(codeGenConfigModel.getOutputDir());
    }

    private void handleCodeGen(CodeGenConfigModel codeGenConfigModel) throws Exception {
        boolean isOriginTableOnly = CodeGenValidator.isOriginTableOnly(codeGenConfigModel);

        codeGenConfigModel.setTemplateConfig(new PiscesTemplateConfigModel(isOriginTableOnly));
        piscesCodeGenProvider.execute(codeGenConfigModel);


    }

    /**
     * 获取配置信息
     *
     * @return
     */
    @Override
    public FrontConfigInfo configInfo(HttpServletRequest request) {
        Integer localRunning = HttpSessionUtil.isLocal(request) ? 1:0;
        FrontConfigInfo frontConfigInfo = new FrontConfigInfo(localRunning);
        return frontConfigInfo;
    }


    @Override
    public PageResultModel<TableInfoModel> listTable(DbTableQueryModel model) {
        SqlUtil sqlUtil = new SqlUtil(model);
        PageResultModel<TableInfoModel> pageResult = sqlUtil.listTableInfo();
        return pageResult;
    }


    @Override
    public R testDbConn(DbTableQueryModel model) {
        String result = DbValidator.valid(model);
        if (R.SUCCESS.equals(result)) {
            return R.ok();
        }
        return R.fail(result);
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/


    /**
     * 代码生成之后的处理
     *
     * @param codeGenConfigModel
     */
    private void handleAfterCodeGen(CodeGenConfigModel codeGenConfigModel) throws Exception {
        if (isOpenDirAfterDone(codeGenConfigModel)) {
            try {
                String osName = System.getProperty("os.name");
                if (osName != null) {
                    if (osName.contains("Mac")) {
                        Runtime.getRuntime().exec("open " + codeGenConfigModel.getOutputDir());
                    } else if (osName.contains("Windows")) {
                        Runtime.getRuntime().exec("cmd /c start " + codeGenConfigModel.getOutputDir());
                    } else {
                    }
                }
            } catch (IOException var4) {
                var4.printStackTrace();
            }
        }
    }

    /**
     * 是否打开生成目录
     *
     * @param codeGenConfigModel
     * @return
     */
    private boolean isOpenDirAfterDone(CodeGenConfigModel codeGenConfigModel) {
        return codeGenConfigModel.getOpenDirAfterDone() != null
                && codeGenConfigModel.getOpenDirAfterDone() == 1;

    }

}
